package hotel;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class ImpostazioniXML {
	private String colore,indirizzoip,porta,password,username,salvato;
	private Node nodo;
	private Document document;
	public ImpostazioniXML(){
		try{
			DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();   
			DocumentBuilder builder = documentFactory.newDocumentBuilder(); 
			document = builder.parse(new File("Risorse/impostazioni.xml"));
			NodeList persone = document.getElementsByTagName("setup");
			nodo = persone.item(0);
			if(nodo.getNodeType() == Node.ELEMENT_NODE) {
				Element imp = (Element)nodo;
				colore=imp.getElementsByTagName("colorefinestra").item(0).getFirstChild().getNodeValue();
				indirizzoip=imp.getElementsByTagName("ipaddress").item(0).getFirstChild().getNodeValue();
				porta=imp.getElementsByTagName("porta").item(0).getFirstChild().getNodeValue();
				salvato=imp.getElementsByTagName("salva").item(0).getFirstChild().getNodeValue();
				if(salvato.equals("si")){
					username=imp.getElementsByTagName("username").item(0).getFirstChild().getNodeValue();
					password=imp.getElementsByTagName("password").item(0).getFirstChild().getNodeValue();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void setUsername(String nuovaUsername){
		Element e=(Element)nodo;
		e.getElementsByTagName("username").item(0).getFirstChild().setNodeValue(nuovaUsername);
		username=nuovaUsername;
		salva();
	}
	
	public void setPassword(String nuovaPassword){
		Element e=(Element)nodo;
		e.getElementsByTagName("password").item(0).getFirstChild().setNodeValue(nuovaPassword);
		password=nuovaPassword;
		salva();
	}
	
	public String getUsername(){
		return username;
	}
	
	public String getPassword(){
		return password;
	}
	
	public void setColore(String nuovoColore){
		Element e=(Element)nodo;
		e.getElementsByTagName("colorefinestra").item(0).getFirstChild().setNodeValue(nuovoColore);
		colore=nuovoColore;
		salva();
	}
	
	public void setIP(String nuovoIP){
		Element e=(Element)nodo;
		e.getElementsByTagName("ipaddress").item(0).getFirstChild().setNodeValue(nuovoIP);
		indirizzoip=nuovoIP;
		salva();
	}
	
	public void setPorta(int nuovaPorta){
		Element e=(Element)nodo;
		e.getElementsByTagName("porta").item(0).getFirstChild().setNodeValue(nuovaPorta+"");
		porta=nuovaPorta+"";
		salva();
	}
	
	private void salva(){
		XMLSerializer serializer = new XMLSerializer();
	    try {
			serializer.setOutputCharStream(new java.io.FileWriter("Risorse/impostazioni.xml"));
			serializer.serialize(document);
		} catch (IOException e) {
			e.printStackTrace();
		}
	    
	}
	
	public void setSalvati(boolean b){
		if(b){
			Element e=(Element)nodo;
			e.getElementsByTagName("salva").item(0).getFirstChild().setNodeValue("si");
			salvato="si";
			salva();
		}else{
			Element e=(Element)nodo;
			e.getElementsByTagName("salva").item(0).getFirstChild().setNodeValue("no");
			setUsername(" ");
			setPassword(" ");
			salvato="no";
			salva();
		}
			
	}
	
	public String getColore(){
		return colore;
	}
	
	public String getIP(){
		return indirizzoip;
	}
	
	public String getPorta(){
		return porta;
	}

	public boolean getSalvati() {
		return salvato.equals("si");
	}
}
